package com.cjjd.c.base;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.cjjd.c.R;
import com.cjjd.c.http.RetrofitInterface;
import com.cjjd.c.http.RetrofitSingleton;


/**
 * ----------------------------------
 * Created by xyd on 2017/3/15.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */
public abstract class BaseActivity extends FragmentActivity {


    /**
     * 全局activity
     */
    protected FragmentActivity mContext;
    protected FragmentManager mFragmentManager;
    protected RetrofitInterface service;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        service = RetrofitSingleton.getService(this);

        mFragmentManager = getSupportFragmentManager();

        if (getContentView() != 0) {
            setContentView(getContentView());
        } else {
            setContentView(R.layout.activity_base);
        }
        if (savedInstanceState == null && getContentFragment() != null) {
            add(getContentFragment());
        }

        if (Build.VERSION.SDK_INT>=21){
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        try {
            super.onPause();
        } catch (Exception e) {
        }
        ;
    }

    @Override
    protected void onStop() {
        try {
            super.onStop();


        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
        } catch (Exception e) {
        }

    }

    public abstract Fragment getContentFragment();

    public int getContentView() {
        return 0;
    }

    public void remove(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .remove(fragment)
                .commit();
    }

    public void show(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .show(fragment)
                .commit();
    }

    public void hide(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .hide(fragment)
                .commit();

    }

    public void add(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
    }

    public void add(Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment, tag)
                .commit();
    }

    public void add(int resId, Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .add(resId, fragment, tag)
                .commit();
    }

    public void replace(Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment, tag)
                .commit();
    }


}
